package com.companyName.sample.service.business;

import com.companyName.sample.bean.dto.sys.OrganizationDto;
import com.companyName.sample.bean.dto.sys.Tree;
import com.companyName.sample.common.util.CommUtil;
import com.companyName.sample.data.entity.OrganizationPo;
import com.companyName.sample.data.mapper.OrganizationMapper;
import com.companyName.sample.service.impl.sys.LogServiceImpl;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guo.jun on 2016/4/11.
 */
@Component
public class OrganizationBiz {
    private static final Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

    @Autowired
    private OrganizationMapper organizationMapper;

    /**
     * 查找机构树
     */
    public List<Tree> findTree() {
        List<Tree> trees = Lists.newArrayList();

        List<OrganizationPo> organizationFather = organizationMapper.findOrganizationAllByPidNull();

        if (organizationFather != null) {
            for (OrganizationPo organizationOne : organizationFather) {
                Tree treeOne = new Tree();

                treeOne.setId(organizationOne.getId());
                treeOne.setText(organizationOne.getName());
                treeOne.setIconCls(organizationOne.getIcon());

                List<OrganizationPo> organizationSon = organizationMapper.findOrganizationAllByPid(organizationOne.getId());

                if (organizationSon != null) {
                    List<Tree> tree = Lists.newArrayList();
                    for (OrganizationPo organizationTwo : organizationSon) {
                        Tree treeTwo = new Tree();
                        treeTwo.setId(organizationTwo.getId());
                        treeTwo.setText(organizationTwo.getName());
                        treeTwo.setIconCls(organizationTwo.getIcon());
                        tree.add(treeTwo);
                    }
                    treeOne.setChildren(tree);
                } else {
                    treeOne.setState("closed");
                }
                trees.add(treeOne);
            }
        }
        return trees;
    }

    public List<OrganizationDto> findTreeGrid() {
        List retlist = new ArrayList<OrganizationDto>();
        List<OrganizationPo> organizationList =  organizationMapper.findOrganizationAll();
        for(OrganizationPo organizationPo:organizationList){
            OrganizationDto organizationDto = new OrganizationDto();
            CommUtil.copyProperties(organizationPo, organizationDto);
            retlist.add(organizationDto);
        }
        return retlist;
    }

    public void addOrganization(OrganizationPo organization) {
        organizationMapper.insert(organization);
    }

    public OrganizationDto findOrganizationById(Long id) {
        OrganizationPo organizationPo = organizationMapper.findOrganizationById(id);
        OrganizationDto organizationDto = new OrganizationDto();
        CommUtil.copyProperties(organizationPo,organizationDto);
        return organizationDto;
    }

    public void updateOrganization(OrganizationPo organization) {
        organizationMapper.updateOrganization(organization);
    }

    public void deleteOrganizationById(Long id) {
        organizationMapper.deleteOrganizationById(id);
    }
}
